<?php
namespace app\common\model;

/**
 * 平台分类模型
 * @author swq
 * datetime 2020-10-20	
*/

class PlatformCategory extends BaseModel 
{
	/**
	 * 平台分类列表
	*/
	public function platformCategoryList()
	{
		return $list = $this->order('pid asc,sort asc')->select()->toArray();
	}

	/**
	 * 添加平台分类
	 * @param params 数据 array
	*/
	public function addPlatformCategory($params = [])
	{
		$data = [
			'pid' => $params['pid'],
			'name' => $params['name'],
			'icon' => $params['icon'],
			'sort' => $params['sort'],
			'status' => $params['status'],
			'create_time' => time()
		];
		if (!$params['id']) {
			$ret = $this->insertGetId($data);
		} else {
			$ret = $this->where(['id' => $params['id']])->update($data);
		}
		return $ret;
	}

	/**
	 * 修改分类显示
	 * @param $data 数据 array
	*/
	public function platformCategoryState($data)
	{
		$info = $this->getOne($data['id']);
		if ($info['status'] == 1) {
			$status = 0;
		}else {
			$status = 1;
		}
		$info->status = $status;
		return $info->save();
	}

	/**
	 * 删除分类
	 * @param id int
	*/
	public function platformCategoryDel($id)
	{
		return $this->where(['id' => $id])->delete();
	}

	/**
     * 无限分类-权限
     * @param $cate            栏目
     * @param string $lefthtml 分隔符
     * @param int $pid         父ID
     * @param int $lvl         层级
     * @return array
     */
    public function cateTree($cate , $lefthtml = '|— ' , $pid = 0 , $level = 0 )
    {
        $arr = array();
        foreach ($cate as $v){
            if ($v['pid'] == $pid) {
                $v['level']      = $level + 1;
                $v['lefthtml'] = str_repeat($lefthtml,$level);
                $v['ltitle']   = $v['lefthtml'].$v['name'];
                $arr[] = $v;
                $arr = array_merge($arr, self::cateTree($cate, $lefthtml, $v['id'], $level+1));
            }
        }
        return $arr;
    }
}